package com.ht.api.call.func.impl.status;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.sql.DataSource;

import com.ht.api.bean.AjaxResult;
import com.ht.api.bean.LoginBean;
import com.ht.api.call.func.IFunctionCall;
import com.ht.api.db.Db;
import com.ht.api.db.DbList;
import com.ht.api.db.Entity;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ht.api.db.exception.HtException;

/**
 * 事件广告配置状态变更
 * @author asq
 * @createTime 2025年3月12日 13:31:46
 */
public class UpdateEventAdConfigStatusFunctionCall implements IFunctionCall {

	@Override
	public String getName() {
		return "事件广告配置状态变更";
	}

	@Override
	public JSONObject execute(LoginBean me, JSONObject param, AjaxResult result) {
		// 获取登陆用户的数据源
		DataSource ds = me.getDataSource();
		
		String configId = param.getStr("main_id");
        int status = param.getInt("status");
        Boolean startEvent = param.getBool("start_event");

        List<Entity> dataList = new ArrayList<>();

        Entity eventAdConfig = Entity.create("event_ad_config");
        eventAdConfig.set("main_id", configId);
        eventAdConfig.set("status", status);
        dataList.add(eventAdConfig);

        DbList eventAdPlanList = Db.createSqlParser("event_ad_plan_config").where("config_id", configId).query(ds);

        String time = DateUtil.now();
        if (status == -1) {
            for (Entity eventAdPlan : eventAdPlanList) {
                eventAdPlan.set("status", -1);
                dataList.add(eventAdPlan);

                Entity plan = Entity.create("ad_plan");
                plan.set("main_id", eventAdPlan.get("plan_id"));
                plan.set("status", 3);
                dataList.add(plan);

                //停用阶段，需要将用户的广告任务全部取消
                DbList taskList = Db.createSqlParser("ad_user_task").select("main_id").where("campaign_id", eventAdPlan.getStr("campaign_id")).where("status",1).query(ds);
                for (Entity task : taskList) {
                    task.set("status", 4);
                    task.set("result_msg", "因事件停用，取消任务");
                    dataList.add(task);
                }

                DbList userList = Db.createSqlParser("ad_plan_user").select("main_id,oneid").where("source_id", eventAdPlan.getMainId()).where("status","in", Arrays.asList(1,2)).query(ds);
                for (Entity user : userList) {
                    user.set("status", 4);
                    dataList.add(user);

                    Entity adPlanUserLogs = Entity.create("ad_plan_user_logs");
                    adPlanUserLogs.set("oneid", user.get("oneid"));
                    adPlanUserLogs.set("plan_id", plan.getMainId());
                    adPlanUserLogs.set("time", time);
                    adPlanUserLogs.set("status", 4);
                    dataList.add(adPlanUserLogs);
                }
            }

        } else if (status == 1) {
            if (startEvent != null && startEvent) {
                for (Entity eventAdPlan : eventAdPlanList) {
                    long count = Db.createSqlParser("ad_campaign_item").where("campaign_id", eventAdPlan.getStr("campaign_id")).count(ds);
                    if (count == 0) {
                        String eventKeyName = Db.createSqlParser("sys_event").select("event_name")
                                .where("event_key", eventAdPlan.getStr("event_key")).val();
                        throw new HtException(eventKeyName + "事件未创建广告策略，不能启动全部事件广告计划！");
                    }

                    eventAdPlan.set("status", 1);
                    dataList.add(eventAdPlan);

                    Entity plan = Entity.create("ad_plan");
                    plan.set("main_id", eventAdPlan.get("plan_id"));
                    plan.set("start_time", DateUtil.now());
                    plan.set("status", 2);
                    dataList.add(plan);
                }
            }

        }

        Db.insertBatchWithStream(me.getStreamLoadConfig(), dataList);

		return JSONUtil.createObj();
	}

}
